<?php 
require('header.php'); 
require('nav.php');

require_once $INCLUDE_DIR.'cametrics.helper.php';
$url = Cametrics::getURL();

$raw = @file_get_contents("$url/contributions/stats/hits?order=desc");
$hits = json_decode($raw, true);
$count = count($hits);

$top = array();
foreach (range(0,2) as $i){
	if (isset($hits[$i])){
		$top[] = $hits[$i];
	}
}

$username = "whatsinvasive"; //flickr user name here
$tagstoignore = array("flickrdate", "zip", "geo:lat", "geo:lon", "geotagged", "shozu", "nolabel", "notvalid", "valid", "invasives", "noninvasives", "oracle", "geolat3406782", "hardinggrass", "perennialpepperweed", "poisonhemlock", "spanishbroom", "terracinaspurge", "yellowstarthistle", "photoerror", "unknownplant");
$mixing = "desc"; // can be random, asc, or desc

// Create new phpFlickr object
require_once($INCLUDE_DIR."phpflickr.helper.php");

//only works if user is rocking a name
if (!empty($username)) {
	//Determine NSID and get tag arra
	$nsid = $f->people_findByUsername($username);
	$tog = $f->tags_getListUserPopular($nsid['id'],1000);	
	
	//determine maximum tagcount
	$i = 0;
	foreach($tog as $togo)
	{
		$sizecheckthing[$i] = $togo['count'];
		$i++;
	}
	
	if ($mixing == "random")
	{
		shuffle($tog); //mess up the mix
	}
	elseif ($mixing == "asc")
	{
		sort($tog); //rank starting with lowest popularity
	}
	elseif ($mixing == "desc")
	{
		rsort($tog); //rank starting with highest popularity
	}

//	Participant List (name, contribution number):<br>';
   
	$db = new PDO('sqlite:'.$DATABASE);

	//make list
	$participant_list = array();
	foreach($tog as $togo)
	{
		$goForward = false;

		//mininum tag count, defaulted to zero
		if ($togo['count'] > 0)
		{
			$a = 0;
			$goForward = false;
		
			foreach ($tagstoignore as $badword)
			{ 
				if (strpos($togo['_content'],$badword)===false) $a++; 
			}

			if ($a == count($tagstoignore)) $goForward = true;

			if ($goForward)
			{		
				$query = "SELECT username FROM user WHERE imei=".$togo['_content'];
echo '<!--'.$query.'-->'.PHP_EOL;
				$result = $db->query($query);
				if (!$result) {
					continue;
				}
				$row = $result->fetch(PDO::FETCH_ASSOC);
				
				$participant_list[$row['username']] = array(
					'_content' => $togo['_content'],
					'count' => $togo['count']);
			}
		}		
	}
	$db = null;
}

$days = json_decode(@file_get_contents($url."contributions/time/stats/dayth"), true);

?>
	<div id="headertext" class="span-24 last">&nbsp;</div>

<!-- Begin Left List -->
	<div class="span-4" style="background-color:white;">
		<?php require('plant_list.php'); ?>
	</div>

<!-- Begin right display -->
	<div id="content" class="span-19 last center"><h3>Campaign Statistics &nbsp;&#8211; &nbsp;Day: <?= count($days) ?></h3><hr /></div>

	<div id="content" class="span-19 last">

		<div class="span-5 colborder center">
			<h3>Weeds Found!</h3>
			<h2><span class="color5"><?= @file_get_contents("$url/contributions/stats/count") ?></span></h2>
			<h3>People Helping!</h3>
			<h2><span class="color5"><?= $count ?></span></h2>
			<h3>Top Image Collectors!</h3>
			<ul class="log">
	<?php foreach($top as $participant): ?>
			<li><strong><span class="color5"><?=$participant[0]?></span>: <?=$participant[1]?></strong></li>
	<?php endforeach ?>
			</ul>
		</div>

		<div class="span-6 colborder center">
			<h3>Users</h3>
			<img src="<?= $url ?>/contributions/stats/hits.gc?cht=p&chs=200x100&chco=054700"/>
			<hr class="space" style="height: 2em;" />
<!--
			<h3>Group Submissions</h3>
<?php 
$grouplist = json_decode(file_get_contents($url."contributions/group/stats/hits"), true);
$numgroup = count($grouplist);
?>
			<img src="<?= $url ?>/contributions/group/stats/hits.gc?cht=bhs&chbh=a&chs=200x<?=$numgroup*25?>&chco=2B5C00"/>
-->
		</div>

		<div class="span-6 last center error">
			Sorry, the rest of the statistics are temporarily unavailable. Please check back here soon!
<!--			<h3>Types of Invasives</h3>
<?php 
$typelist = json_decode(file_get_contents($url."contributions/type/stats/hits"), true);
$numtype = count($typelist);
?>
			<img src="<?= $url ?>/contributions/type/stats/hits.gc?cht=bhs&chbh=a&chs=200x<?=$numtype*25?>&chco=8EA100"/>
-->
		</div>

		<div class="span-19 last"><hr />
			<span style="font-size: 1.5em; marginfont-weight: bold; padding: 0px 0px 0px 10px;">Participants:</span> &nbsp;|

			<?php foreach($participant_list as $username => $datum): ?>
			<a href="http://www.flickr.com/photos/<?= $nsid['id'] ?>/tags/<?= $datum['_content'] ?>" target="_blank"><?= $username ?></a> | 
			<?php endforeach ?>
			<hr class="space" />

			<span style="font-size: 1.5em; marginfont-weight: bold; padding: 0px 0px 0px 10px;">Tags:</span> &nbsp;|
			<a href="http://www.flickr.com/photos/<?= $nsid['id'] ?>/tags/nolabel/" target="_blank">no label</a> |
			<a href="http://www.flickr.com/photos/<?= $nsid['id'] ?>/tags/notvalid/" target="_blank">not valid</a> |
			<a href="http://www.flickr.com/photos/<?= $nsid['id'] ?>/tags/photoerror/" target="_blank">photo error</a> |
			<a href="http://www.flickr.com/photos/<?= $nsid['id'] ?>/tags/noninvasives/" target="_blank">non-invasives</a> |
			<a href="http://www.flickr.com/photos/<?= $nsid['id'] ?>/tags/invasives/" target="_blank">invasives</a> |
		</div>
	</div>
<?php require('footer.php'); ?>
